//
//  ST_AIRecommendEnjoyView.swift
//  btvideo
//
//  Created by mac on 2020/7/8.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit


class ST_AIRecommendEnjoyView: UIControl {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(gradient_view)
        gradient_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        let leftEdge = 8/375.0*ScreenWidth
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.height.equalTo(content_label.font.lineHeight + 3)
            make.left.equalToSuperview().offset(leftEdge)
            make.bottom.equalToSuperview().offset(-8)
        }
        
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(titleLabel.font.lineHeight + 4)
            make.left.equalToSuperview().offset(leftEdge)
            make.bottom.equalTo(content_label.snp.top).offset(-1)
        }
        
        self.addSubview(left_label)
        left_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(leftEdge)
            make.right.equalToSuperview().offset(-leftEdge)
            make.height.equalTo(left_label.font.lineHeight + 7)
            make.width.equalTo(40)
        }
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        print("GGG0---",self.titleLabel.frame)
        print("GGG1---",self.content_label.frame)
        print("GGG2---",self.frame)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK:- Properties
    var item_model:ST_AIRecommendEnjoyViewModel?{
        didSet{
            guard let model = item_model else{return}
            
            gradient_view.model = ST_FWGradientViewModel.init(colors: model.gradientColors, startPoint: CGPoint.init(x: 1, y: 0), endPoint: CGPoint.init(x: 0, y: 1))
            
            titleLabel.text = model.title
            content_label.text = model.content
            
            left_label.text = model.leftTitle
            left_label.isHidden = model.leftTitle.isEmpty
            
            left_label.snp.updateConstraints { (make) in
                make.width.equalTo(model.leftTitle.width(withConstrainedHeight: 100, font: left_label.font)+12)
            }
            
            
            guard  let path = Bundle.main.path(forResource: model.animationName, ofType: "json") else{
                return
            }
                   
           let animationControl = LOTAnimationView.init(filePath: path)
            animationControl.isUserInteractionEnabled = false
           animationControl.contentMode = UIView.ContentMode.scaleAspectFill
           
           animationControl.loopAnimation = true
            
            self.insertSubview(animationControl, aboveSubview: gradient_view)
            animationControl.snp.makeConstraints { (make) in
                make.edges.equalToSuperview()
            }
            self.animateView?.removeFromSuperview()
            self.animateView = animationControl
            
            animationControl.play()
            
        }
    }
    
    fileprivate lazy var gradient_view:ST_FWGradientView = {
            
        let control = ST_FWGradientView()
        control.cornerRadius = 10
        control.isUserInteractionEnabled = false
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: 14/375.0*ScreenWidth, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var left_label:UILabel = {
        let control = UILabel()
        control.text = ""
        control.textAlignment = .center
        control.cornerRadius = 8
        control.backgroundColor = UIColor.init(white: 0, alpha: 0.2)
        control.textColor = .white
        control.font = UIFont.systemFont(ofSize: 9/375.0*ScreenWidth, weight: UIFont.Weight.medium)
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: 11/375.0*ScreenWidth, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate var animateView:LOTAnimationView?
}


//MARK: - model
struct ST_AIRecommendEnjoyViewModel {
    var title:String
    var content:String
    var leftTitle:String
    var animationName:String
    var gradientColors:[UIColor]
}
